<template>
  <div>
    <el-form
      :model="queryEquipmentParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="70px"
    >
      <el-form-item label="设备ID" prop="equipmentId">
        <el-input
          v-model="queryEquipmentParams.equipmentId"
          placeholder="设备ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryEquipmentParams.equipmentName"
          placeholder="设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addTask"
          >创建任务</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="18">
      <el-col :span="8">
        <el-table
          ref="equipmentTable"
          :data="equipmentList"
          :height="tableHeight + 68"
          highlight-current-row
          @current-change="handleCurrentEquipmentChange"
          border
        >
          <template slot="empty">
            <div>
              <span>设备不存在</span>
            </div>
          </template>
          <el-table-column label="序号" width="50px">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>

          <el-table-column
            label="设备名称"
            align="center"
            prop="equipmentName"
          />
          <el-table-column
            label="设备状态"
            align="center"
            prop="equipmentName"
          />
        </el-table>
        <pagination
          v-show="total > 0"
          small
          layout="prev, pager, next"
          :total="total"
          :page.sync="queryEquipmentParams.pageNum"
          :limit.sync="queryEquipmentParams.pageSize"
          @pagination="getList"
        />
      </el-col>

      <el-col :span="16">
        <div>
          <div class="title-task">已完成任务：</div>
          <el-table :data="taskList" :height="tableHeight / 2" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="参数ID"
              align="center"
              prop="equipmentParamId"
            />
          </el-table>

          <pagination
            v-show="finishedTotal > 0"
            :total="finishedTotal"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getTaskList(0)"
          />
        </div>
        <div>
          <div class="title-task">排队任务：</div>
          <el-table :data="unfinishedTaskList" :height="tableHeight / 2" border>
            <el-table-column label="任务ID" align="center" prop="workingId" />
            <el-table-column
              label="任务名称"
              align="center"
              prop="workingName"
            />
            <el-table-column
              label="任务状态"
              align="center"
              prop="status"
              :formatter="statusFormat"
            />
            <el-table-column label="开始时间" align="center" prop="startTime" />
            <el-table-column label="结束时间" align="center" prop="endTime" />
          </el-table>

          <pagination
            v-show="unfinishedTotal > 0"
            :total="unfinishedTotal"
            :page.sync="unfinishedQueryParams.pageNum"
            :limit.sync="unfinishedQueryParams.pageSize"
            @pagination="getTaskList(1)"
          />
        </div>
      </el-col>
    </el-row>
    <model-add
      ref="modelAddRef"
      @changeFun="changeFun"
      :equipmentOptions="equipmentOptions"
      :deptOptions="deptOptions"
      :materialAddMethodOptions="materialAddMethodOptions"
    />
  </div>
</template>

<script>
import { listMaterialAddMethod } from "@/api/technology/working";
import { listEquipment } from "@/api/equipment/param";
import { getIndexTecholagy } from "@/api/index";
import ModelAdd from "../../technology/workingMaintain/components/ModelAdd.vue";
import { listDeptOptions, getEquipmentOptionsAll } from "@/api/equipment/info";
export default {
  name: "Param",
  components: { ModelAdd },
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      equipmentOptions: [],
      deptOptions: [],
      materialAddMethodOptions: [],
      // 总条数
      unfinishedTotal: 0,
      finishedTotal: 0,
      // 设备参数表格数据
      taskList: [],
      unfinishedTaskList: [],
      allEquipmentList: [],
      // 设备列表
      equipmentList: [],
      tableHeight: 350,
      currentEquipmentId: null,
      // 查询参数
      queryEquipmentParams: {
        equipmentId: null,
        equipmentName:'',
        equipmentCategoryId: 1,
        pageNum: 1,
        pageSize: 10,
      },
      unfinishedQueryParams: {
        equipmentId: null,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
      },
      statusOptions: [],
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    // 任务状态字典
    this.getDicts("technology_work_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getEquipmentOptions(); //设备下拉
    this.getDeptOptions(); //公司下拉
    this.getMaterialAddMethodOptions(); //增材方式
  },
  destroyed() {
    window.removeEventListener("resize", this.getHeight);
  },

  mounted() {
    this.queryEquipmentParams.equipmentId = this.$route.query.equipmentId;
    this.getList();
    this.allEquipmentList = this.equipmentList;
  },
  methods: {
    changeFun() {
        this.getList()
    },
    getMaterialAddMethodOptions() {
      // listMaterialAddMethod().then((response) => {
      //   this.materialAddMethodOptions = response.rows;
      // });
      this.getDicts("base_material_addMethod").then((response) => {
      this.materialAddMethodOptions = response.data;
    });
    },
    getEquipmentOptions() {
      getEquipmentOptionsAll().then((response) => {
        this.equipmentOptions = response.rows;
      });
    },
    /* 获取公司列表 */
    getDeptOptions() {
      listDeptOptions().then((response) => {
        this.deptOptions = response.data;
      });
    },
    //   创建任务
    addTask() {
      this.$refs.modelAddRef.addFun();
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询设备参数列表 */
    getList() {
      listEquipment(this.queryEquipmentParams).then((response) => {
        this.equipmentList = response.rows;
        this.total = response.total;

        if (this.equipmentList.length > 0) {
          this.handleCurrentEquipmentChange(this.equipmentList[0]);
        }
      });
    },
    getTaskList(status) {
      let obj = {
        equipmentId: this.currentEquipmentId,
        status: status,
      };
      let parmas={}
      if(status==0){
         parmas={...this.queryParams,...obj}
      }else{
        parmas={...this.unfinishedQueryParams,...obj}
      }
      getIndexTecholagy(parmas).then((response) => {
        if (status == 0) {
          //已完成任务
          this.taskList = response.rows;
          this.finishedTotal = response.total;
        } else {
          this.unfinishedTaskList = response.rows;
          this.unfinishedTotal = response.total;
        }
      });
    },
    getHeight() {
      this.tableHeight = window.innerHeight - 275;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryEquipmentParams.pageNum = 1;
      this.taskList = [];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.currentEquipmentId = null;
      this.queryEquipmentParams.equipmentName = null;
      this.queryEquipmentParams.equipmentId = null;

      this.handleQuery();
    },
    handleCurrentEquipmentChange(selection) {
      if (selection != null) {
        this.currentEquipmentId = selection.equipmentId;
        this.queryParams.equipmentId = selection.equipmentId;

        this.getTaskList(0);
        this.getTaskList(1);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table th {
  display: table-cell !important;
}
.title-task {
  padding-bottom: 10px;
}
</style>
